package com.esender.ordermanage.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    // 准备普通队列和死信队列，基于死信队列完成延迟消息
    public static final String ORDER_EXCHANGE = "order_exchange";
    public static final String ORDER_QUEUE = "order_queue";

    public static final String DEAD_EXCHANGE = "dead_exchange";
    public static final String DEAD_QUEUE = "dead_queue";

    @Bean
    public Exchange orderExchange() {
        return ExchangeBuilder.fanoutExchange(ORDER_EXCHANGE).build();
    }
    @Bean
    public Queue orderQueue() {
        // 普通队列绑定死信交换机
        return QueueBuilder.durable(ORDER_QUEUE).deadLetterExchange(DEAD_EXCHANGE).build();
        //return QueueBuilder.durable(ORDER_QUEUE).build();
    }
    /**
     * 以下是声明死信队列
     */
    @Bean
    public Exchange deadExchange() {
        return ExchangeBuilder.fanoutExchange(DEAD_EXCHANGE).build();
    }
    @Bean
    public Queue deadQueue() {
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }
    /**
     * 以下是准备普通队列与普通交换机的绑定，死信队列和死信交换机的绑定
     */
    @Bean
    public Binding orderBinding(Exchange orderExchange, Queue orderQueue) {
        return BindingBuilder.bind(orderQueue).to(orderExchange).with("").noargs();
    }
    @Bean
    public Binding deadBinding(Exchange deadExchange, Queue deadQueue) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("").noargs();
    }
}
